﻿package 
{
	import flash.display.Sprite;	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.core.geom.Pixels;
	import org.papervision3d.core.geom.renderables.Pixel3D;
	import org.papervision3d.materials.WireframeMaterial;
	import org.papervision3d.objects.primitives.Sphere;
	import org.papervision3d.render.BasicRenderEngine;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.view.layer.BitmapEffectLayer;
	import org.papervision3d.view.Viewport3D;
	
	/*
	 * coded by moka 2009 / 06/ 25 
	 * name : moka's MathParticle
	 */
	
	public class Main extends Sprite 
	{		
		private var scene:Scene3D;
		private var camera:Camera3D;
		private var viewport:Viewport3D;
		private var render:BasicRenderEngine;
		private var pixelLayer:BitmapEffectLayer;
		private var pixels:Pixels;
		private var degree:Number = 0;
		private var ball:Sphere;		
		private var factorX:Number = 0;
		private var factorY:Number = 0;
		private var factorZ:Number = 0;

		public function Main():void 
		{				
			
			// 設定場景不變形
			stage.scaleMode = "noScale"; 
			
			// Scene3D 用來組織場景母子關係樹狀架構
 			scene = new Scene3D();

			// Camera3D 為 3D 場景可視範圍設定
			camera = new Camera3D();
			
			
			// Viewport3D 用在呈現於 flash 場景(其類別繼承Sprite)
			viewport = addChild(new Viewport3D(640, 480))as Viewport3D;
			viewport.filters = [new GlowFilter(0xFFFFFF, 1, 3, 3, 5, 3),new GlowFilter(0x00AAFF, 1, 20, 20, 5, 3)];
			
			// 用以渲染 3D 場景之類別
			render = new BasicRenderEngine();
			
			// 新增 pixelLayer 讓 pixel 繪製上去
			pixelLayer = new BitmapEffectLayer(viewport, 640, 480);
			pixelLayer.clearBeforeRender = true; // 清除畫面再繪製
			viewport.containerSprite.addLayer(pixelLayer); // 讓 Pixels 可以被繪製出來
			pixels = scene.addChild(new Pixels(pixelLayer)) as Pixels; // Pixels 物件加入場景
			pixels.z = 1000;

			// pixel會參考球的頂點繪製
			ball = new Sphere(new WireframeMaterial, 1000, 80, 60);
			
			for (var i:uint = 0; i < ball.geometry.vertices.length; i++) {				
				pixels.addPixel3D(new Pixel3D(0xCCFFFFFF, 0, 0, 0));
			}
			
			stage.addEventListener(Event.ENTER_FRAME, onProcess);	
			stage.addEventListener(MouseEvent.MOUSE_DOWN, function onMD(e:MouseEvent):void { 
				factorX = Math.random()*10-5;
				factorY = Math.random()*10-5;
				factorZ = Math.random()*10-5;
			});

			(addChild(new TextField()) as TextField).htmlText = "<font color='#FFFFFF'>Moka's work 2009</font>"; 
		}
			
		private function onProcess(e:Event):void {	
			// 旋轉 pixels 物件
			pixels.rotationX = mouseY;
			pixels.rotationZ = mouseX;

			degree++;		

			// pixel 參照頂點作變化
			for(var i:uint = 0; i < ball.geometry.vertices.length; i++)
			{
				pixels.pixels[i].x = ball.geometry.vertices[i].x*Math.pow(Math.sin((degree + i) / 180 * Math.PI),factorX);
				pixels.pixels[i].y = ball.geometry.vertices[i].y*Math.pow(Math.cos((degree + i) / 180 * Math.PI),factorY);				
				pixels.pixels[i].z = ball.geometry.vertices[i].z*Math.pow(Math.cos((degree + i) / 180 * Math.PI),factorZ);			
			}
						
			// 渲染 3D 場景
			render.renderScene(scene, camera, viewport);
		}
	}	
}